using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Beta_forums_controls_threads_moderatethread : Controls.DataManagerThreadControl
{
    public event EventHandler ThreadUpdated;

    private DAL.User CurrentUser
    {
        get
        {
            return Managers.UserLoginManager.CurrentUser;
        }
    }

    protected override void ItemChanged()
    {
        Refresh();
    }

    protected override void UserLoggedIn(object sender, EventArgs e)
    {
        Refresh();
    }

    protected override void UserLoggedOut(object sender, EventArgs e)
    {
        Refresh();
    }

    private void Refresh()
    {
        pnlMod.Visible = false;

        if (CurrentThread != null)
        {
            if (CurrentUser != null && CurrentUser.HasPermissions(DAL.User.UserStatus.Moderator))
            {
                pnlMod.Visible = true;
                switch (CurrentThread.Status)
                {
                    case DAL.Thread.ThreadStatus.Deleted:
                        ddlStatus.SelectedValue = "-1";
                        break;
                    case DAL.Thread.ThreadStatus.Locked:
                        ddlStatus.SelectedValue = "0";
                        break;
                    default:
                        ddlStatus.SelectedValue = "1";
                        break;
                }

                switch (CurrentThread.Priority)
                {
                    case DAL.Thread.ThreadPriority.Sticky:
                        ddlPriority.SelectedValue = "1";
                        break;
                    default:
                        ddlPriority.SelectedValue = "0";
                        break;
                }

                ForumListBox1.CurrentForum = CurrentThread.Forum;
            }
        }
    }

    protected void ChangeStatus(object sender, EventArgs e)
    {
        switch (ddlStatus.SelectedValue)
        {
            case "0":
                CurrentThread.Status = DAL.Thread.ThreadStatus.Locked;
                break;
            case "-1":
                CurrentThread.Status = DAL.Thread.ThreadStatus.Deleted;
                break;
            default:
                CurrentThread.Status = DAL.Thread.ThreadStatus.Open;
                break;
        }
        SaveThread();
    }

    private void SaveThread()
    {
        DAL.Threads.Save(CurrentThread);
        if (CurrentThread.Status == DAL.Thread.ThreadStatus.Deleted)
        {
            UpdateForum();
        }
        if (ThreadUpdated != null)
        {
            ThreadUpdated(this, new EventArgs());
        }
    }

    private void UpdateForum()
    {
        CurrentThread.Forum.TotalThreads -= 1;
        CurrentThread.Forum.TotalPosts -= CurrentThread.TotalReplies + 1;
        DAL.Forums.Save(CurrentThread.Forum);
    }

    protected void ForumSelected(object sender, EventArgs e)
    {
        CurrentThread.Forum = ForumListBox1.CurrentForum;
        SaveThread();
    }
    protected void ChangePriority(object sender, EventArgs e)
    {
        switch (ddlPriority.SelectedValue)
        {
            case "1":
                CurrentThread.Priority = DAL.Thread.ThreadPriority.Sticky;
                break;
            default:
                CurrentThread.Priority = DAL.Thread.ThreadPriority.Normal;
                break;
        }
        SaveThread();
    }
}
